package com.example.android.ui.personal;

import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.android.R;
import com.example.android.ui.base.BaseActivity;
import com.example.android.utils.CircleImageView;

import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class PersonalActivity extends BaseActivity implements View.OnClickListener, PersonalContract.IPersonalView {
    private ImageView back;
    private PersonalPresenter personalPresenter;
    private Button commit;
    private EditText account,username,gender,birthday;
    private CircleImageView headImage;
    @Override
    protected void initViews() {

        personalPresenter = new PersonalPresenter(this);
        back = find(R.id.personal_back);
        commit = find(R.id.personal_commit);
        account = find(R.id.personal_edit_account);
        username = find(R.id.personal_edit_username);
        gender = find(R.id.personal_edit_gender);
        birthday = find(R.id.personal_edit_birth);
        headImage = find(R.id.personal_image);
        getAndLoadData();
        //点击事件
        back.setOnClickListener(this);
        commit.setOnClickListener(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.personal_back:
                finish();
                break;
            case R.id.personal_commit:
                SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin",0);
                int userId = sharedPreferences.getInt("userId",0);
                String token = sharedPreferences.getString("jwt","");
                HashMap<String, Object> map = new HashMap<>();
                map.put("userId",userId);
                map.put("userUsername",username.getText().toString());
                map.put("userBirthDatetime",birthday.getText().toString());
//                Log.i("PersonalActivity",userId + " " + username.getText().toString() + " jwt:"+token );
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), new JSONObject(map).toString());
                personalPresenter.setPersonalData(requestBody,token);
                break;
            default:
                break;
        }
    }

    @Override
    public void setPersonalDateSuccess(boolean bool) {
        if (bool == true){
            Toast.makeText(PersonalActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin",0);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("userUsername",username.getText().toString());
            editor.putString("userBirthDatetime",birthday.getText().toString());
            editor.commit();
            finish();
        }else{
            Toast.makeText(PersonalActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void setPersonalDateError(Throwable throwable) {
        Log.e("PersonalActivity",throwable.toString());
    }
    public void getAndLoadData(){
        SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin",0);
        String userUsername = sharedPreferences.getString("userUsername","");
        String userAccount = sharedPreferences.getString("userAccount","");
        String userGender = sharedPreferences.getString("userGender","男");
        String userHeadimageurl = sharedPreferences.getString("userHeadimageurl","");
        String userBirthday = sharedPreferences.getString("userBirthday","");
//        Log.i("PersonalActivity",userBirthday+" "+userUsername);
        account.setText(userAccount);
        username.setText(userUsername);
        gender.setText(userGender);
        birthday.setText(userBirthday);
        Glide.with(this).load(userHeadimageurl).into(headImage);

    }

}
